package com.lufei.cardin.config;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CodeGeneral_test {
    public static void main(String[] args) {
        String dirPath="mybits";//新库只需要修改这里

        String path = System.getProperty("user.dir") + "/src/main/java";
        String path2 = System.getProperty("user.dir") + "/src/main/java/com/lufei/cardin/mybits";
        List<String> tables = new ArrayList<>();




       tables.add("permission");
        tables.add("role");
        tables.add("role_permission");
        tables.add("token_user");
        tables.add("user");
        tables.add("user_role");



        FastAutoGenerator.create("jdbc:p6spy:mysql://49.232.175.171:3306/shiro?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&autoReconnect=true&failOverReadOnly=false&maxReconnects=10"
                        , "lufei", "6418019")
                .globalConfig(builder -> {
                    builder.author("路飞4")               //作者
                            .disableOpenDir()
                            .outputDir(path)    //输出路径(写到java目录)
                            .enableSpringdoc()           //开启swagger
                            .commentDate("yyyy-MM-dd");


                })
                .packageConfig(builder -> {
                    builder.parent("com.lufei.cardin"+".mybits")
                            .moduleName("mybatisPlus")
                            .entity("entity")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller")
                            .mapper("mapper")
                            .xml("mapper")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, path2));
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tables)
                            .addTablePrefix("p_")
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .entityBuilder()
                            .enableLombok()
                            //策略
                            .logicDeleteColumnName("deleted")
                            .versionColumnName("version")
                            .addTableFills(new Column("createTime", FieldFill.INSERT))    //基于数据库字段填充
                            .addTableFills(new Column("updateTime", FieldFill.INSERT_UPDATE))    //基于模型属性填充

                            .enableFileOverride()//覆盖现有文件

                            .enableTableFieldAnnotation()
                            .controllerBuilder()
                            // 映射路径使用连字符格式，而不是驼峰
                            .enableHyphenStyle()
                            .formatFileName("%sController")
                            .enableRestStyle()
                            .mapperBuilder()
                            //生成通用的resultMap
                            .enableBaseResultMap()
                            .superClass(BaseMapper.class)
                            .formatMapperFileName("%sMapper")
                            .enableMapperAnnotation()
                            .formatXmlFileName("%sMapper");
                })
//                .templateConfig(new Consumer<TemplateConfig.Builder>() {
//                    @Override
//                    public void accept(TemplateConfig.Builder builder) {
//                        // 实体类使用我们自定义模板
//                        builder.entity("templates/myentity.java");
//                    }
//                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();

    }


    private static void xxx() {

        List<String> tables = new ArrayList<>();
        tables.add("permission");
        tables.add("role");
        tables.add("role_permission");
        tables.add("user");
        tables.add("user_role");
        String path = System.getProperty("user.dir") + "/src/main/java";
        String path2 = System.getProperty("user.dir") + "/src/main/java/com/example/mp/shiro/outputDir";
        FastAutoGenerator.create("jdbc:p6spy:mysql://8.130.21.209:3306/shiro?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&autoReconnect=true&failOverReadOnly=false&maxReconnects=10"
                        , "lufei"
                        , "6418019")
                .globalConfig(builder -> {
                    builder.author("路飞") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .disableOpenDir()
                            .outputDir(path); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.example.mp.shiro") // 设置父包名
                            .moduleName("mybatis_plus") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, path2)); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tables) // 设置需要生成的表名
                            .addTablePrefix("t_", "c_"); // 设置过滤表前缀
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();

    }
}
